package cn.iocoder.yudao.module.member.service.scorerecord;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.controller.admin.scorerecord.vo.ScoreRecordCreateReqVO;
import cn.iocoder.yudao.module.member.controller.admin.scorerecord.vo.ScoreRecordExportReqVO;
import cn.iocoder.yudao.module.member.controller.admin.scorerecord.vo.ScoreRecordPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.scorerecord.vo.ScoreRecordUpdateReqVO;
import cn.iocoder.yudao.module.member.convert.scorerecord.ScoreRecordConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.scorerecord.ScoreRecordDO;
import cn.iocoder.yudao.module.member.dal.mysql.scorerecord.ScoreRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.SCORE_RECORD_NOT_EXISTS;

/**
 * 用户积分兑换 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ScoreRecordServiceImpl implements ScoreRecordService {

    @Resource
    private ScoreRecordMapper scoreRecordMapper;

    @Override
    public Long createScoreRecord(ScoreRecordCreateReqVO createReqVO) {
        // 插入
        ScoreRecordDO scoreRecord = ScoreRecordConvert.INSTANCE.convert(createReqVO);
        scoreRecordMapper.insert(scoreRecord);
        // 返回
        return scoreRecord.getId();
    }

    @Override
    public void updateScoreRecord(ScoreRecordUpdateReqVO updateReqVO) {
        // 校验存在
        validateScoreRecordExists(updateReqVO.getId());
        // 更新
        ScoreRecordDO updateObj = ScoreRecordConvert.INSTANCE.convert(updateReqVO);
        scoreRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteScoreRecord(Long id) {
        // 校验存在
        validateScoreRecordExists(id);
        // 删除
        scoreRecordMapper.deleteById(id);
    }

    private void validateScoreRecordExists(Long id) {
        if (scoreRecordMapper.selectById(id) == null) {
            throw exception(SCORE_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public ScoreRecordDO getScoreRecord(Long id) {
        return scoreRecordMapper.selectById(id);
    }

    @Override
    public List<ScoreRecordDO> getScoreRecordList(Collection<Long> ids) {
        return scoreRecordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ScoreRecordDO> getScoreRecordPage(ScoreRecordPageReqVO pageReqVO) {
        return scoreRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ScoreRecordDO> getScoreRecordList(ScoreRecordExportReqVO exportReqVO) {
        return scoreRecordMapper.selectList(exportReqVO);
    }

}
